﻿window.onload = function () {
  var btn = document.querySelector(".slide-btn");
  var box = document.querySelector(".slide-box");
  var bg = document.querySelector(".slide-bg");
  var text = document.querySelector(".slide-text");
  var flag = false;
  btn.onmousedown = function (e) {//按下
    var downX = e.clientX; //按下后对x轴的距离
    //	    console.log(downX);
    //	    alert("1");
    btn.onmousemove = function (e) {//拖动
      var moveX = e.clientX - downX; //拖动后与x轴距离减去初始值距离，移动值
      //		console.log(moveX);

      //移动范围
      if (moveX > -2) {
        this.style.left = moveX + "px";//将移动值赋值给滑块
        bg.style.width = moveX + "px";//背景
        if (moveX >= (box.offsetWidth - btn.offsetWidth)) {//包含原始宽度内边距边框，不包含外边框
          //拖到头，验证成功
          flag = true;
          text.innerHTML = "验证成功";
          text.style.color = "white";
          //事件清除
          btn.onmousedown = null;
          btn.onmousemove = null;
        }
      }
    }
  }

  //松开按钮
  btn.onmouseup = function () {
    //事件清除
    btn.onmousemove = null;
    if (flag) return;
    this.style.left = 0;//将移动值赋值给滑块
    bg.style.width = 0;//背景

  }
}



//获取用户名以及密码并提交
function submitLogin() {
  //TODO 判断输入框是否为空，为空则禁止提交

  //获取md5加密的密码
  pwd_hash = hex_md5($("#pd").val());

  //TODO 获取是否记住密码的checkBox
  //提交
  //uname 用户名
  //pd 密码
  //isSave 是否保存密码
  //接下来从前端输入获取以上几个参数，目前暂时写死
  $.post("cotro/login/forLogin.ashx",
    {
      indiv_id: $("#username").val(),
      pd: pwd_hash,
      isSave: "true"
    },
    //回调
    function (res, status) {
      res = JSON.parse(res);
      console.log(res);
      //判断是否登录成功
      if (res.code == -1) {
        alert(res.msg);
        return;
      }
      //成功后,判断跳转页面
      //判断用户类型并返回固定页面
      //角色为管理员
      if (res.role == 3) {
        window.location = "./Admin.aspx";
        $.cookie('indiv_id', res.indiv_id, { expires: 7 });
      }
      //角色为学生
      if (res.role == 1) {
        window.location = "./Student.aspx";
        $.cookie('indiv_id', res.indiv_id, { expires: 7 });
        }
        //角色为教师
        if (res.role == 2) {
            window.location = "./Teacher.aspx";
            $.cookie('indiv_id', res.indiv_id, { expires: 7 });
        }
    }
  );
}



